﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ESOL.Business.Mst;
using ESOL.Common;
using ESOL.Common.Entity;

namespace ESOL.WinUI.Sales
{
    public partial class frmMaintainCustomerQuery : BaseForm.frmBaseDialogyQueryForm
    {
        #region Variable
        private MstCustomerEntity mSelectedCustomer;
        public MstCustomerEntity SelectedCustomer
        {
            get { return mSelectedCustomer; }
        }
        private MstPersonEntity mSelectedPerson;
        private bool mShowSupplierOnly = false;
        public bool ShowSupplierOnly
        {
            get { return mShowSupplierOnly; }
            set { mShowSupplierOnly = value; }
        }
        private bool mShowAllCustomer = false;
        public bool ShowAllCustomer
        {
            get { return mShowAllCustomer; }
            set { mShowAllCustomer = value; }
        }
        #endregion

        #region Constructor
        public frmMaintainCustomerQuery()
        {
            InitializeComponent();
            mSelectedPerson = CommonParameter.CurrentUser.PersonEntity;
        }
        public frmMaintainCustomerQuery(MstPersonEntity person)
        {
            InitializeComponent();
            mSelectedPerson = person;
        }
        #endregion

        #region Functions
        private void BindCombobox()
        {
            UICommon.UICommonFunctions.BindGridComboboxByCommonItem(cLevel, "CL", true);
            UICommon.UICommonFunctions.BindComboboxByCommonItem(cmbStatus, "STATUS", false);
        }

        private void SetField()
        {
            cmbStatus.SetSelectedValue("STATUS03");
        }

        private string GetCondition()
        {
            string condition = "";
            if (mSelectedPerson != null && mShowAllCustomer == false)
            {
                condition += @" AND (D.CONTACT_AGE = -1 OR D.CUSTOMER_ID IN (SELECT CUSTOMER_ID FROM MST_PERSON_CUSTOMER PC
						LEFT JOIN MST_PERSON P ON P.PERSON_ID = PC.PERSON_ID
                        LEFT JOIN MST_PERSON P2 ON P2.PERSON_ID = P.SUPERVISOR
						LEFT JOIN MST_PERSON P3 ON P3.PERSON_ID = P2.SUPERVISOR
						LEFT JOIN MST_PERSON P4 ON P4.PERSON_ID = P3.SUPERVISOR
						LEFT JOIN MST_PERSON P5 ON P5.PERSON_ID = P4.SUPERVISOR
                        WHERE PC.PERSON_ID = '" + mSelectedPerson.PersonId + @"'
	                    OR P2.PERSON_ID = '" + mSelectedPerson.PersonId + @"'
	                    OR P3.PERSON_ID = '" + mSelectedPerson.PersonId + @"'
	                    OR P4.PERSON_ID = '" + mSelectedPerson.PersonId + @"'
	                    OR P5.PERSON_ID = '" + mSelectedPerson.PersonId + @"'))";
            }
            if (string.IsNullOrEmpty(txtCustomerCode.Text) == false)
            {
                condition += " AND D.CUSTOMER_CODE LIKE '%" + txtCustomerCode.Text + "%' ";
            }
            if (string.IsNullOrEmpty(txtCoustomerName.Text) == false)
            {
                condition += " AND D.CUSTOMER_NAME LIKE '%" + txtCoustomerName.Text + "%' ";
            }
            if (string.IsNullOrEmpty(txtCustomerFullName.Text) == false)
            {
                condition += " AND D.CUSTOMER_FULL_NAME LIKE '%" + txtCustomerFullName.Text + "%' ";
            }
            if (mShowSupplierOnly)
            {
                condition += " AND D.CONTACT_AGE = -1 ";
            }
            if (string.IsNullOrEmpty(cmbStatus.SelectedValue.ToString()) == false)
            {
                condition += " AND D.STATUS='"+cmbStatus.SelectedValue.ToString()+"' ";
            }
            return condition;
        }

        private void BindDataGrid()
        {
            string condition = GetCondition();
            string orderBy = "CUSTOMER_CODE";
            DataTable dt = MstCustomerBs.GetCustomerByCondition(condition, orderBy, "");
            gvMain.DataSource = dt;
        }
        #endregion

        #region Event
        protected override void OnShown(EventArgs e)
        {
            BindCombobox();
            SetField();
            BindDataGrid();
            base.OnShown(e);
        }

        protected override void Click_Query()
        {
            BindDataGrid();
        }

        protected override void Click_Confirm()
        {
            if (mSelectedCustomer == null)
            {
                MessageBox.Show("请选择一个客户！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            this.DialogResult = DialogResult.OK;
        }

        private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            DataRow dr = gridView1.GetDataRow(e.RowHandle);
            mSelectedCustomer = new MstCustomerEntity(dr);
        }

        private void gridView1_DoubleClick(object sender, EventArgs e)
        {
            if (mSelectedCustomer == null)
            {
                MessageBox.Show("请选择一个客户！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            this.DialogResult = DialogResult.OK;
        }
        #endregion
    }
}
